#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/6/11 10:50
# @Author  : fuchen'en
# @Description:

import pika
import sys

credentials = pika.PlainCredentials('test', 'test123')
parameters = pika.ConnectionParameters(host='10.0.20.66',
                                       port='5672',
                                       virtual_host='/',
                                       credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

# 声明具名交换机，类型为fanout，广播机制
channel.exchange_declare(exchange='logs', exchange_type='fanout')

message = ' '.join(sys.argv[1:]) or "info: Hello World!"

# fanout机制下，不需要指定路由，交换机会广播给所有连接到MQ的消费者
channel.basic_publish(exchange='logs', routing_key='', body=message)
print(" [x] Sent {!r}".format(message))
connection.close()
